# 创建测试数据的PowerShell脚本
Write-Host "开始创建测试数据..."

# 使用POST方法创建数据
$headers = @{
    "Content-Type" = "application/json"
}

# 创建用户数据
Write-Host "创建用户数据..."
$userData = @(
    @{key="user_001"; value="user_data_admin"}
    @{key="user_002"; value="user_data_manager"}
    @{key="user_003"; value="user_data_employee"}
    @{key="user_004"; value="user_data_admin"}
    @{key="user_005"; value="user_data_guest"}
)

foreach ($data in $userData) {
    $body = $data | ConvertTo-Json
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:8080/api/v1/kv/get-or-create" -Method POST -Body $body -Headers $headers
        Write-Host "创建成功: $($data.key) = $($data.value)"
    } catch {
        Write-Host "创建失败: $($data.key) - $($_.Exception.Message)"
    }
}

# 创建产品数据
Write-Host "创建产品数据..."
$productData = @(
    @{key="product_001"; value="product_laptop"}
    @{key="product_002"; value="product_mouse"}
    @{key="product_003"; value="product_laptop"}
    @{key="product_004"; value="product_keyboard"}
    @{key="product_005"; value="product_monitor"}
)

foreach ($data in $productData) {
    $body = $data | ConvertTo-Json
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:8080/api/v1/kv/get-or-create" -Method POST -Body $body -Headers $headers
        Write-Host "创建成功: $($data.key) = $($data.value)"
    } catch {
        Write-Host "创建失败: $($data.key) - $($_.Exception.Message)"
    }
}

# 创建订单数据
Write-Host "创建订单数据..."
$orderData = @(
    @{key="order_001"; value="order_pending"}
    @{key="order_002"; value="order_completed"}
    @{key="order_003"; value="order_pending"}
    @{key="order_004"; value="order_cancelled"}
    @{key="order_005"; value="order_completed"}
)

foreach ($data in $orderData) {
    $body = $data | ConvertTo-Json
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:8080/api/v1/kv/get-or-create" -Method POST -Body $body -Headers $headers
        Write-Host "创建成功: $($data.key) = $($data.value)"
    } catch {
        Write-Host "创建失败: $($data.key) - $($_.Exception.Message)"
    }
}

Write-Host "测试数据创建完成！"

# 模拟一些访问以增加访问计数
Write-Host "模拟用户访问..."
for ($i = 1; $i -le 5; $i++) {
    Invoke-WebRequest -Uri "http://localhost:8080/api/v1/kv/user_001" -Method GET | Out-Null
    Invoke-WebRequest -Uri "http://localhost:8080/api/v1/kv/user_004" -Method GET | Out-Null
    Invoke-WebRequest -Uri "http://localhost:8080/api/v1/kv/product_001" -Method GET | Out-Null
    Invoke-WebRequest -Uri "http://localhost:8080/api/v1/kv/product_003" -Method GET | Out-Null
}
Write-Host "访问模拟完成"

# 获取统计信息
Write-Host "获取统计信息..."
try {
    $stats = Invoke-WebRequest -Uri "http://localhost:8080/api/v1/kv/stats" -Method GET
    Write-Host "统计信息: $($stats.Content)"
} catch {
    Write-Host "获取统计信息失败: $($_.Exception.Message)"
}

Write-Host "脚本执行完成！"